// 定义好类后,若一些私有属性我们是不能直接访问的,或者某些属性我们想要监听它的获取(getter)和设置(setter)的过程
class Person {
    private _name: string
    set name(newName) {
        this._name = newName
    }
    get name() {
        return this._name
    }
    constructor(name: string) {
        this._name = name
    }
}
const p = new Person("why")
p.name = "codywhy"
console.log(p.name)

// getters和setters应用
class Person1 {
    firstName: string //姓氏
    lastName: string //名字
    constructor(firstName: string, lastName: string) {
        console.log('在实例化对象的时候,构造器被执行')
        this.firstName = firstName
        this.lastName = lastName
    }
    // 对数据进行读取
    get fullName() {
        console.log('get方法被执行了')
        return this.firstName + "-" + this.lastName
    }
    // 对数据进行修改
    set fullName(str: string) {
        console.log('set方法被执行了')
        this.firstName = str.split('-')[0]
        this.lastName = str.split('-')[1]
    }
}
const per = new Person1('张', '无忌');
// 会去执行get方法,因为获取值吗
console.log(per.fullName)
// 会去执行set方法；因为你修改了值
per.fullName = '李-四'



export { }